package com.sanhe.rfmuscle.activity.work;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;


import com.sanhe.rfmuscle.R;
import com.sanhe.rfmuscle.databinding.ActivityConfigBinding;
import com.sanhe.rfmuscle.event.A1Event;
import com.sanhe.rfmuscle.event.A2Event;
import com.sanhe.rfmuscle.event.A3Event;
import com.sanhe.rfmuscle.event.B1Event;
import com.sanhe.rfmuscle.event.B2Event;
import com.sanhe.rfmuscle.event.B3Event;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.event.RfShowEvent;
import com.sanhe.rfmuscle.event.VrefEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ConfigActivity extends BaseWorkActivity {

    ActivityConfigBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityConfigBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x20));
        binding.ok.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x44));

        binding.rfOn.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xac));
        binding.rfOff.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xad));

        binding.aMaxDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa1));
        binding.aMaxUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa0));

        binding.aMinDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa3));
        binding.aMinUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa2));

        binding.a3Down.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa5));
        binding.a3Up.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa4));

        binding.bMaxDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa7));
        binding.bMaxUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa6));

        binding.bMinDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa9));
        binding.bMinUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xa8));

        binding.b3Down.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xab));
        binding.b3Up.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xaa));

        binding.vrefDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xaf));
        binding.vrefUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0xae));

        binding.ok.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg_press);
                }else if (event.getAction() == MotionEvent.ACTION_UP) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg);
                }
                return false;
            }
        });

        updateState();
        updateA1();
        updateA2();
        updateA3();
        updateB1();
        updateB2();
        updateB3();
        updateVref();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfShowEvent event) {
        updateState();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(A1Event event) {
        updateA1();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(A2Event event) {
        updateA2();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(A3Event event) {
        updateA3();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(B1Event event) {
        updateB1();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(B2Event event) {
        updateB2();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(B3Event event) {
        updateB3();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(VrefEvent event) {
        updateVref();
    }

    private void updateA1() {
        binding.aMax.setText(Event.a1Event.getValue() + "");
    }

    private void updateA2() {
        binding.aMin.setText(Event.a2Event.getValue() + "");
    }

    private void updateA3() {
        binding.a3.setText(Event.a3Event.getValue() + "");
    }

    private void updateB1() {
        binding.bMax.setText(Event.b1Event.getValue() + "");
    }

    private void updateB2() {
        binding.bMin.setText(Event.b2Event.getValue() + "");
    }

    private void updateB3() {
        binding.b3.setText(Event.b3Event.getValue() + "");
    }

    private void updateVref() {
        binding.vref.setText(Event.vrefEvent.getValue() + "");
    }

    private void updateState() {
        if (Event.rfShowEvent.getValue() == 1) {
            binding.rfOff.setVisibility(View.INVISIBLE);
            binding.rfOn.setVisibility(View.VISIBLE);

        } else {
            binding.rfOff.setVisibility(View.VISIBLE);
            binding.rfOn.setVisibility(View.INVISIBLE);
        }
    }
}
